//
//  SignUPInforVC.m
//  doubleEduStudents
//
//  Created by 盛洋 on 2021/1/14.
//  Copyright © 2021 shengYang. All rights reserved.
//

#import "SignUPInforVC.h"
#import "signInforCollectionCell.h"
#import "signInforCollectionCell1.h"
#import "signInforCollectionCell2.h"
#import "signInforctionViewCell3.h"
#import "DownLoadVC.h"
#import "GreatProductioVC.h"

@interface SignUPInforVC ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (strong, nonatomic) IBOutlet UICollectionView *collection_view;
@property (strong, nonatomic) IBOutlet UIImageView *back_img;
@property (strong, nonatomic) IBOutlet UIButton *baoMing_btn;
@property (strong, nonatomic) IBOutlet UIButton *back_btn;

@end

@implementation SignUPInforVC
{
    NSMutableArray *paraiseArr;//奖品数组
    RequestServices *service;
    NSMutableDictionary *valueDic;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"作品详情";
    self.collection_view.delegate = self;
    self.collection_view.dataSource = self;
    [self.collection_view registerNib:[UINib nibWithNibName:@"signInforCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"signInforCollectionCell"];
    [self.collection_view registerNib:[UINib nibWithNibName:@"signInforCollectionCell1" bundle:nil] forCellWithReuseIdentifier:@"signInforCollectionCell1"];
    [self.collection_view registerNib:[UINib nibWithNibName:@"signInforCollectionCell2" bundle:nil] forCellWithReuseIdentifier:@"signInforCollectionCell2"];
    [self.collection_view registerNib:[UINib nibWithNibName:@"signInforctionViewCell3" bundle:nil] forCellWithReuseIdentifier:@"signInforctionViewCell3"];
    
    //回到顶部
    [self.back_btn addTarget:self action:@selector(backToTop) forControlEvents:UIControlEventTouchUpInside];
    
    [self createNavigationBarRightBarButtonItemWithTitle:@"分享" font:18 image:nil target:self action:@selector(shareCurrent)];
    
    paraiseArr = [NSMutableArray array];
    service = [[RequestServices alloc]initWithDelegate:self];
    service.view = self.view;
    [service getGymInfor:self.typeID tag:8000];
}


-(void)getFinished:(NSDictionary *)obj tag:(long long)tag{
    [self HideLoading];
    if (tag == 8000) {
        valueDic = [NSMutableDictionary dictionaryWithDictionary:obj];
        if ([obj[@"matchPrizeEntity"] count] > 0) {
            paraiseArr = [ParaiseModel mj_objectArrayWithKeyValuesArray:obj[@"matchPrizeEntity"]];
        }
    }
    [self.collection_view reloadData];
}

-(void)getError:(NSError *)error tag:(long long)tag{
    [self HideLoading];
    [self ShowNewMsg:error.domain];
}

-(void)backToTop{
     [self.collection_view setContentOffset:CGPointMake(0, 0) animated:YES];
}

-(void)shareCurrent{
    [[OpenAPIServices sharedService]shareInfo:self urlString:@"https://www.baidu.com" titleString:@"测试" titleImage:[UIImage imageNamed:@""] desc:@"这是一款非常好用的软件" infoID:@"5555"];
}

#pragma mark ----- collectionViewDataSource

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    if ([valueDic CheckNSDictionaryData]) {
        return 4;
    }else{
        return 0;
    }
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if ([valueDic CheckNSDictionaryData]) {
        
        if (section == 3){
            return 1+paraiseArr.count;
        }else{
            return 1;
        }}else{
            return 0;
        }
}

//每个item的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if ([valueDic CheckNSDictionaryData]) {
        
        if (indexPath.section == 2){
            return CGSizeMake(CollectionView_Width/2, CollectionView_Height* 0.9);
        }else if (indexPath.section == 3){
            if (indexPath.item == 0) {
                return CGSizeMake(CollectionView_Width/3, CollectionView_Height* 0.8 *0.15);
            }else{
                return CGSizeMake(CollectionView_Width/3, CollectionView_Height* 0.2);
            }
        }else{
            return CGSizeMake(CollectionView_Width/2, CollectionView_Height * 0.9);
        }
    }else{
        return CGSizeMake(0, 0);
    }
}
//cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([valueDic CheckNSDictionaryData]) {
        if (indexPath.section == 0) {
            signInforCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"signInforCollectionCell" forIndexPath:indexPath];
            //立即报名上传作品
            if ([valueDic[@"status"] integerValue] == 1){
                [cell.join_btn addTarget:self action:@selector(shangChuan) forControlEvents:UIControlEventTouchUpInside];
            }
            //优秀作品展示
            if ([valueDic[@"status"] integerValue] == 3){
                [cell.join_btn addTarget:self action:@selector(showProduction) forControlEvents:UIControlEventTouchUpInside];
            }
            cell.dic = valueDic;
            return cell;
        }else if (indexPath.section == 1){
            signInforCollectionCell1 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"signInforCollectionCell1" forIndexPath:indexPath];
            cell.dic = valueDic;
            return cell;
        }else if (indexPath.section == 2){
            signInforCollectionCell2 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"signInforCollectionCell2" forIndexPath:indexPath];
            if ([valueDic[@"matchPhoto"] count] > 0) {
                cell.imgArr = valueDic[@"matchPhoto"];                
            }
            NSString *webVideoPath = [[NSString alloc]init];
            //步骤1：获取视频路径
            if ([self.model.video containsString:@"http"]) {
                webVideoPath  = valueDic[@"video"];
            }else{
                webVideoPath =[NSString stringWithFormat:@"https://%@",valueDic[@"video"]];
            }
            NSURL *webVideoUrl = [NSURL URLWithString:webVideoPath];
            //步骤2：创建AVPlayer
            AVPlayer *avPlayer = [[AVPlayer alloc] initWithURL:webVideoUrl];
            //步骤3：使用AVPlayer创建AVPlayerViewController，并跳转播放界面
            AVPlayerViewController *avPlayerVC =[[AVPlayerViewController alloc] init];
            avPlayerVC.player = avPlayer;
            //视频sizeFill
            avPlayerVC.videoGravity = AVLayerVideoGravityResize;
            //设置播放速度
            //        avPlayer.rate=1.0;
            //跳到指定时间点播放
            [avPlayer seekToTime:CMTimeMake(1,1)];
            //步骤4：设置播放器视图大小
            avPlayerVC.view.frame = cell.back_view.bounds;
            //特别注意:AVPlayerViewController不能作为局部变量被释放，否则无法播放成功
            //解决1.AVPlayerViewController作为属性
            //解决2:使用addChildViewController，AVPlayerViewController作为子视图控制器
            [self addChildViewController:avPlayerVC];
            [cell.back_view addSubview:avPlayerVC.view];
            __weak AVPlayer *_avPlayer;
            _avPlayer = avPlayer;
            [avPlayer addPeriodicTimeObserverForInterval:CMTimeMake(1,1) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
                float total1 = _avPlayer.currentItem.duration.value;//帧数
                float total2 = _avPlayer.currentItem.duration.timescale;//每秒帧数
                //总时长
                float totalTime  = total1/total2;
                NSLog(@"--帧数-%.1f--秒帧数-%.1f--总时间%.1f-",total1,total2,totalTime);
                //当前时间
                float current = time.value * 1.0f/time.timescale;
                NSLog(@"--观看时间--%f",current);
            }];
            return cell;
        }
        else{
            signInforctionViewCell3 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"signInforctionViewCell3" forIndexPath:indexPath];
            if (indexPath.row == 0) {
                cell.center_lab.text = @"奖品列表";
                cell.center_lab.textColor = [UIColor whiteColor];
                cell.center_lab.font = AutoLabFont2;
                cell.pradise_img.image = [UIImage imageNamed:@""];
            }else{
                if (paraiseArr.count > 0) {
                    cell.center_lab.text = @"";
                    ParaiseModel *model = paraiseArr[indexPath.item-1];
                    [cell.pradise_img sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.photo]]];
                }
            }
            return cell;
        }}else{
            return nil;
        }
}


//列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

//四周边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 20, 0, 0);
}

//上传作品
-(void)shangChuan{
    DownLoadVC *vc = [[DownLoadVC alloc]init];
    vc.type = [valueDic[@"type"] integerValue];
    vc.typeid = [valueDic[@"id"] integerValue];
    [self PushToViewController:vc];
}

//优秀作品
-(void)showProduction{
    GreatProductioVC *vc = [[GreatProductioVC alloc]init];
    vc.type = [valueDic[@"type"] integerValue];
    vc.typeID = [valueDic[@"id"] integerValue];
    [self PushToViewController:vc];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
